package curtain.whynot.frame.basic.csv.impl;

import com.whynot.frame.basic.util.PatternMatchUtils;
import curtain.whynot.frame.basic.csv.annotation.CsvConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;


@CsvConverter
public abstract class BaseConverter {

	private static final Logger logger = LoggerFactory.getLogger(BaseConverter.class);

	/**
	 * 是否导出
	 * @return
	 */
	protected static boolean isExportReq() {
		HttpServletRequest request = getRequest();
		if(null == request){
			return false;
		}
		String uri = request.getRequestURI();
		logger.debug("export import url :" + uri);
		return PatternMatchUtils.simpleMatch("*/export", uri )||uri.endsWith("/sqlexport");
	}

	/**
	 * 是否导入
	 * @return
	 */
	protected static boolean isImportReq() {
		HttpServletRequest request = getRequest();
		if(null == request){
			return false;
		}
		String uri = request.getRequestURI();
		logger.debug("export/import url :" + uri);

		return PatternMatchUtils.simpleMatch("*/import", uri )||uri.endsWith("/csv/exportInValidRecord")||uri.endsWith("/sqlimport") ;
	}

	/**
	 * 获取request信息，从而获得用户登录信息及浏览器访问信息
	 *
	 * @return
	 */
	protected static HttpServletRequest getRequest() {
		HttpServletRequest request = CsvImportImpl.getRequest(); 
//		if(request==null) {
//			request = WebUtil.getRequest();
//		}
		return request;
	}

	/**
	 * @return
	 */
	public static String allwaysTrue(Object param) {
		logger.debug("param :" + param);
		return "true" ;
	}

	/**
	 * @return
	 */
	public static String allwaysFalse(Object param) {
		logger.debug("param :" + param);
		CsvImportImpl.setErrmsg("始终验证不通过");
		return "false" ;
	}


}
